package info.demo.view;

import javax.servlet.jsp.PageContext;
import java.util.Map;
import java.util.Set;
import java.util.Collection;

public class AttributeFinder implements Map<String, Object> {

    private PageContext pageContext;


    public PageContext getPageContext() {
        return pageContext;
    }

    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    public int size() {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public boolean isEmpty() {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public boolean containsKey(Object key) {
        if (pageContext == null)
            throw new IllegalStateException("The PageContext was not initialized");
        return pageContext.findAttribute(key.toString()) != null;
    }

    public boolean containsValue(Object value) {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public Object get(Object key) {
        if (pageContext == null)
            throw new IllegalStateException("The PageContext was not initialized");
        return pageContext.findAttribute(key.toString());
    }

    public Object put(String key, Object value) {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public Object remove(Object key) {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public void putAll(Map<? extends String, ? extends Object> m) {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public void clear() {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public Set<String> keySet() {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public Collection<Object> values() {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

    public Set<Entry<String, Object>> entrySet() {
        throw new UnsupportedOperationException("This is intended to use only in JSP with JSTL");
    }

}
